<template>
	<view class="login-container">
		<view class="login-header">
			<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
			<text class="title">WMS系统</text>
			<text class="subtitle">仓库管理系统</text>
		</view>

		<view class="login-form">
			<view class="input-group">
				<view class="input-item">
					<text class="input-label">账号</text>
					<input class="input-field" type="text" placeholder="请输入账号" v-model="username" :focus="focusUsername"
						@focus="focusUsername = true" @blur="focusUsername = false" />
				</view>

				<view class="input-item">
					<text class="input-label">密码</text>
					<input class="input-field" type="password" placeholder="请输入密码" v-model="password" :focus="focusPassword"
						@focus="focusPassword = true" @blur="focusPassword = false" />
				</view>
			</view>

			<view class="login-options">
				<label class="remember-me">
					<checkbox :checked="rememberMe" @change="rememberMe = !rememberMe" color="#2979ff" />
					<text>记住密码</text>
				</label>
				<text class="forgot-password" @click="forgotPassword">忘记密码？</text>
			</view>

			<button class="login-btn" @click="handleLogin" :disabled="!canLogin" :loading="loading">
				登录
			</button>
		</view>

		<view class="login-footer">
			<text class="footer-text">© 2024 WMS系统 版权所有</text>
		</view>
	</view>
</template>

<script>
import { mapActions } from 'vuex'
import api from '@/api'
import JSEncrypt from "jsencrypt";
import UUID from "uuid-js";

export default {
	data() {
		return {
			username: '229661797',
			password: '123456',
			rememberMe: false,
			focusUsername: false,
			focusPassword: false,
			loading: false
		}
	},
	computed: {
		canLogin() {
			return this.username.trim() && this.password.trim()
		}
	},
	onLoad() {
		// 检查是否已经登录
		if (this.$store.getters['auth/isLoggedIn']) {
			uni.reLaunch({
				url: '/pages/index/index'
			})
			return
		}

		// 检查是否有保存的登录信息
		this.loadSavedCredentials()
	},
	methods: {
		...mapActions('auth', ['login']),

		async handleLogin() {
			if (!this.canLogin) {
				uni.showToast({
					title: '请输入账号和密码',
					icon: 'none'
				})
				return
			}

			// 显示加载状态
			uni.showLoading({
				title: '登录中...'
			})
			this.loading = true;
			try {
				// 调用登录API
				const result = await api.user.login("publicKey", {});
				uni.setStorageSync("pbKey", result.data.publicKey)

				var encrypt = new JSEncrypt();
				encrypt.setPublicKey(uni.getStorageSync('pbKey'));
				let secretkey = UUID.create().toString();
				uni.setStorageSync("secretkey", secretkey);
				
				const response = await api.user.login("templogin", {
					userid: this.username,
					password: encrypt.encrypt(this.password),
					guidorg: '11c366ff38ff470aa46826751597e4d8',
					logintype: "",
					secretkey: encrypt.encrypt(secretkey)
				});
				if(!response.data.status){
					uni.showToast(response.data.message)
					return
				}

				// 使用Vuex保存登录信息
				this.login(response.data.data)

				// 保存登录信息
				if (this.rememberMe) {
					this.saveCredentials()
				} else {
					this.clearCredentials()
				}

				// 登录成功，跳转到首页
				uni.reLaunch({
					url: '/pages/index/index'
				})

			} catch (error) {
				uni.showToast({
					title: error.message || '登录失败',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
				this.loading = false;
			}
		},

		saveCredentials() {
			uni.setStorageSync('savedCredentials', {
				username: this.username,
				password: this.password,
				rememberMe: true
			})
		},

		clearCredentials() {
			uni.removeStorageSync('savedCredentials')
		},

		loadSavedCredentials() {
			const saved = uni.getStorageSync('savedCredentials')
			if (saved && saved.rememberMe) {
				this.username = saved.username || ''
				this.password = saved.password || ''
				this.rememberMe = true
			}
		},

		forgotPassword() {
			uni.showToast({
				title: '请联系管理员重置密码',
				icon: 'none'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.login-container {
	min-height: 100vh;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	display: flex;
	flex-direction: column;
	padding: 0 40rpx;
}

.login-header {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding-top: 100rpx;

	.logo {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 40rpx;
	}

	.title {
		font-size: 48rpx;
		font-weight: bold;
		color: #ffffff;
		margin-bottom: 20rpx;
	}

	.subtitle {
		font-size: 28rpx;
		color: rgba(255, 255, 255, 0.8);
	}
}

.login-form {
	background: #ffffff;
	border-radius: 20rpx;
	padding: 60rpx 40rpx;
	margin-bottom: 60rpx;
	box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);

	.input-group {
		margin-bottom: 40rpx;
	}

	.input-item {
		margin-bottom: 30rpx;

		.input-label {
			display: block;
			font-size: 28rpx;
			color: #333333;
			margin-bottom: 15rpx;
			font-weight: 500;
		}

		.input-field {
			width: 95%;
			height: 80rpx;
			border: 2rpx solid #e5e5e5;
			border-radius: 10rpx;
			padding: 0 20rpx;
			font-size: 28rpx;
			background: #fafafa;
			transition: all 0.3s ease;

			&:focus {
				border-color: #2979ff;
				background: #ffffff;
				box-shadow: 0 0 0 4rpx rgba(41, 121, 255, 0.1);
			}
		}
	}

	.login-options {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 40rpx;

		.remember-me {
			display: flex;
			align-items: center;
			font-size: 26rpx;
			color: #666666;

			text {
				margin-left: 10rpx;
			}
		}

		.forgot-password {
			font-size: 26rpx;
			color: #2979ff;
			text-decoration: underline;
		}
	}

	.login-btn {
		width: 100%;
		height: 88rpx;
		background: linear-gradient(135deg, #2979ff 0%, #1e88e5 100%);
		color: #ffffff;
		border: none;
		border-radius: 10rpx;
		font-size: 32rpx;
		font-weight: bold;
		transition: all 0.3s ease;

		&:active {
			transform: scale(0.98);
		}

		&:disabled {
			background: #cccccc;
			color: #999999;
		}
	}
}

.login-footer {
	padding-bottom: 40rpx;
	text-align: center;

	.footer-text {
		font-size: 24rpx;
		color: rgba(255, 255, 255, 0.6);
	}
}
</style>